home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / implicit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  19.0 KB  |  613 lines

  1. /* Implicit rule searching for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,94,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "rule.h"
  21. #include "dep.h"
  22. #include "filedef.h"
  23.  
  24. static int pattern_search PARAMS ((struct file *file, int archive, unsigned int depth,
  25.         unsigned int recursions));
  26.  
  27. /* For a FILE which has no commands specified, try to figure out some
  28.    from the implicit pattern rules.
  29.    Returns 1 if a suitable implicit rule was found,
  30.    after modifying FILE to contain the appropriate commands and deps,
  31.    or returns 0 if no implicit rule was found.  */
  32.  
  33. int
  34. try_implicit_rule (file, depth)
  35.      struct file *file;
  36.      unsigned int depth;
  37. {
  38.   DEBUGPR ("Looking for an implicit rule for `%s'.\n");
  39.  
  40.   /* The order of these searches was previously reversed.  My logic now is
  41.      that since the non-archive search uses more information in the target
  42.      (the archive search omits the archive name), it is more specific and
  43.      should come first.  */
  44.  
  45.   if (pattern_search (file, 0, depth, 0))
  46.     return 1;
  47.  
  48. #ifndef    NO_ARCHIVES
  49.   /* If this is an archive member reference, use just the
  50.      archive member name to search for implicit rules.  */
  51.   if (ar_name (file->name))
  52.     {
  53.       DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
  54.       if (pattern_search (file, 1, depth, 0))
  55.     return 1;
  56.     }
  57. #endif
  58.  
  59.   return 0;
  60. }
  61.  
  62. #define DEBUGP2(msg, a1, a2)                              \
  63.   do {                                          \
  64.     if (debug_flag)                                  \
  65.       { print_spaces (depth); printf (msg, a1, a2); fflush (stdout); }          \
  66.   } while (0)
  67.  
  68. /* Search the pattern rules for a rule with an existing dependency to make
  69.    FILE.  If a rule is found, the appropriate commands and deps are put in FILE
  70.    and 1 is returned.  If not, 0 is returned.
  71.  
  72.    If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)".  A rule for
  73.    "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
  74.    directory and filename parts.
  75.  
  76.    If an intermediate file is found by pattern search, the intermediate file
  77.    is set up as a target by the recursive call and is also made a dependency
  78.    of FILE.
  79.  
  80.    DEPTH is used for debugging messages.  */
  81.  
  82. static int
  83. pattern_search (file, archive, depth, recursions)
  84.      struct file *file;
  85.      int archive;
  86.      unsigned int depth;
  87.      unsigned int recursions;
  88. {
  89.   /* Filename we are searching for a rule for.  */
  90.   char *filename = archive ? index (file->name, '(') : file->name;
  91.  
  92.   /* Length of FILENAME.  */
  93.   unsigned int namelen = strlen (filename);
  94.  
  95.   /* The last slash in FILENAME (or nil if there is none).  */
  96.   char *lastslash;
  97.  
  98.   /* This is a file-object used as an argument in
  99.      recursive calls.  It never contains any data
  100.      except during a recursive call.  */
  101.   struct file *intermediate_file = 0;
  102.  
  103.   /* List of dependencies found recursively.  */
  104.   struct file **intermediate_files
  105.     = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
  106.  
  107.   /* List of the patterns used to find intermediate files.  */
  108.   char **intermediate_patterns
  109.     = (char **) alloca (max_pattern_deps * sizeof (char *));
  110.  
  111.   /* This buffer records all the dependencies actually found for a rule.  */
  112.   char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
  113.   /* Number of dep names now in FOUND_FILES.  */
  114.   unsigned int deps_found;
  115.  
  116.   /* Names of possible dependencies are constructed in this buffer.  */
  117.   register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
  118.  
  119.   /* The start and length of the stem of FILENAME for the current rule.  */
  120.   register char *stem;
  121.   register unsigned int stemlen;
  122.  
  123.   /* Buffer in which we store all the rules that are possibly applicable.  */
  124.   struct rule **tryrules
  125.     = (struct rule **) alloca (num_pattern_rules * max_pattern_targets
  126.                    * sizeof (struct rule *));
  127.  
  128.   /* Number of valid elements in TRYRULES.  */
  129.   unsigned int nrules;
  130.  
  131.   /* The numbers of the rule targets of each rule
  132.      in TRYRULES that matched the target file.  */
  133.   unsigned int *matches
  134.     = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
  135.  
  136.   /* Each element is nonzero if LASTSLASH was used in
  137.      matching the corresponding element of TRYRULES.  */
  138.   char *checked_lastslash
  139.     = (char *) alloca (num_pattern_rules * sizeof (char));
  140.  
  141.   /* The index in TRYRULES of the rule we found.  */
  142.   unsigned int foundrule;
  143.  
  144.   /* Nonzero if should consider intermediate files as dependencies.  */
  145.   int intermed_ok;
  146.  
  147.   /* Nonzero if we have matched a pattern-rule target
  148.      that is not just `%'.  */
  149.   int specific_rule_matched = 0;
  150.  
  151.   register unsigned int i;
  152.   register struct rule *rule;
  153.   register struct dep *dep;
  154.  
  155.   char *p, *vp;
  156.  
  157. #ifndef    NO_ARCHIVES
  158.   if (archive || ar_name (filename))
  159.     lastslash = 0;
  160.   else
  161. #endif
  162.     {
  163.       /* Set LASTSLASH to point at the last slash in FILENAME
  164.      but not counting any slash at the end.  (foo/bar/ counts as
  165.      bar/ in directory foo/, not empty in directory foo/bar/.)  */
  166. #ifdef VMS
  167.       lastslash = rindex (filename, ']');
  168. #else
  169.       lastslash = rindex (filename, '/');
  170. #ifdef __MSDOS__
  171.       /* Handle backslashes (possibly mixed with forward slashes)
  172.      and the case of "d:file".  */
  173.       {
  174.     char *bslash = rindex (filename, '\\');
  175.     if (lastslash == 0 || bslash > lastslash)
  176.       lastslash = bslash;
  177.     if (lastslash == 0 && filename[0] && filename[1] == ':')
  178.       lastslash = filename + 1;
  179.       }
  180. #endif
  181. #endif
  182.       if (lastslash != 0 && lastslash[1] == '\0')
  183.     lastslash = 0;
  184.     }
  185.  
  186.   /* First see which pattern rules match this target
  187.      and may be considered.  Put them in TRYRULES.  */
  188.  
  189.   nrules = 0;
  190.   for (rule = pattern_rules; rule != 0; rule = rule->next)
  191.     {
  192.       /* If the pattern rule has deps but no commands, ignore it.
  193.      Users cancel built-in rules by redefining them without commands.  */
  194.       if (rule->deps != 0 && rule->cmds == 0)
  195.     continue;
  196.  
  197.       /* If this rule is in use by a parent pattern_search,
  198.      don't use it here.  */
  199.       if (rule->in_use)
  200.     {
  201.       DEBUGP2 ("Avoiding implicit rule recursion.%s%s\n", "", "");
  202.       continue;
  203.     }
  204.  
  205.       for (i = 0; rule->targets[i] != 0; ++i)
  206.     {
  207.       char *target = rule->targets[i];
  208.       char *suffix = rule->suffixes[i];
  209.       int check_lastslash;
  210.  
  211.       /* Rules that can match any filename and are not terminal
  212.          are ignored if we're recursing, so that they cannot be
  213.          intermediate files.  */
  214.       if (recursions > 0 && target[1] == '\0' && !rule->terminal)
  215.         continue;
  216.  
  217.       if (rule->lens[i] > namelen)
  218.         /* It can't possibly match.  */
  219.         continue;
  220.  
  221.       /* From the lengths of the filename and the pattern parts,
  222.          find the stem: the part of the filename that matches the %.  */
  223.       stem = filename + (suffix - target - 1);
  224.       stemlen = namelen - rule->lens[i] + 1;
  225.  
  226.       /* Set CHECK_LASTSLASH if FILENAME contains a directory
  227.          prefix and the target pattern does not contain a slash.  */
  228.  
  229. #ifdef VMS
  230.       check_lastslash = lastslash != 0 && index (target, ']') == 0;
  231. #else
  232.       check_lastslash = lastslash != 0 && index (target, '/') == 0;
  233. #endif
  234.       if (check_lastslash)
  235.         {
  236.           /* In that case, don't include the
  237.          directory prefix in STEM here.  */
  238.           unsigned int difference = lastslash - filename + 1;
  239.           if (difference > stemlen)
  240.         continue;
  241.           stemlen -= difference;
  242.           stem += difference;
  243.         }
  244.  
  245.       /* Check that the rule pattern matches the text before the stem.  */
  246.       if (check_lastslash)
  247.         {
  248.           if (stem > (lastslash + 1)
  249.           && strncmp (target, lastslash + 1, stem - lastslash - 1))
  250.         continue;
  251.         }
  252.       else if (stem > filename
  253.            && strncmp (target, filename, stem - filename))
  254.         continue;
  255.  
  256.       /* Check that the rule pattern matches the text after the stem.
  257.          We could test simply use streq, but this way we compare the
  258.          first two characters immediately.  This saves time in the very
  259.          common case where the first character matches because it is a
  260.          period.  */
  261.       if (*suffix != stem[stemlen]
  262.           || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
  263.         continue;
  264.  
  265.       /* Record if we match a rule that not all filenames will match.  */
  266.       if (target[1] != '\0')
  267.         specific_rule_matched = 1;
  268.  
  269.       /* A rule with no dependencies and no commands exists solely to set
  270.          specific_rule_matched when it matches.  Don't try to use it.  */
  271.       if (rule->deps == 0 && rule->cmds == 0)
  272.         continue;
  273.  
  274.       /* Record this rule in TRYRULES and the index of the matching
  275.          target in MATCHES.  If several targets of the same rule match,
  276.          that rule will be in TRYRULES more than once.  */
  277.       tryrules[nrules] = rule;
  278.       matches[nrules] = i;
  279.       checked_lastslash[nrules] = check_lastslash;
  280.       ++nrules;
  281.     }
  282.     }
  283.  
  284.   /* If we have found a matching rule that won't match all filenames,
  285.      retroactively reject any non-"terminal" rules that do always match.  */
  286.   if (specific_rule_matched)
  287.     for (i = 0; i < nrules; ++i)
  288.       if (!tryrules[i]->terminal)
  289.     {
  290.       register unsigned int j;
  291.       for (j = 0; tryrules[i]->targets[j] != 0; ++j)
  292.         if (tryrules[i]->targets[j][1] == '\0')
  293.           break;
  294.       if (tryrules[i]->targets[j] != 0)
  295.         tryrules[i] = 0;
  296.     }
  297.  
  298.   /* Try each rule once without intermediate files, then once with them.  */
  299.   for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
  300.     {
  301.       /* Try each pattern rule till we find one that applies.
  302.      If it does, copy the names of its dependencies (as substituted)
  303.      and store them in FOUND_FILES.  DEPS_FOUND is the number of them.  */
  304.  
  305.       for (i = 0; i < nrules; i++)
  306.     {
  307.       int check_lastslash;
  308.  
  309.       rule = tryrules[i];
  310.  
  311.       /* RULE is nil when we discover that a rule,
  312.          already placed in TRYRULES, should not be applied.  */
  313.       if (rule == 0)
  314.         continue;
  315.  
  316.       /* Reject any terminal rules if we're
  317.          looking to make intermediate files.  */
  318.       if (intermed_ok && rule->terminal)
  319.         continue;
  320.  
  321.       /* Mark this rule as in use so a recursive
  322.          pattern_search won't try to use it.  */
  323.       rule->in_use = 1;
  324.  
  325.       /* From the lengths of the filename and the matching pattern parts,
  326.          find the stem: the part of the filename that matches the %.  */
  327.       stem = filename
  328.         + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
  329.       stemlen = namelen - rule->lens[matches[i]] + 1;
  330.       check_lastslash = checked_lastslash[i];
  331.       if (check_lastslash)
  332.         {
  333.           stem += lastslash - filename + 1;
  334.           stemlen -= (lastslash - filename) + 1;
  335.         }
  336.  
  337.       DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
  338.            (int) stemlen, stem);
  339.  
  340.       /* Try each dependency; see if it "exists".  */
  341.  
  342.       deps_found = 0;
  343.       for (dep = rule->deps; dep != 0; dep = dep->next)
  344.         {
  345.           /* If the dependency name has a %, substitute the stem.  */
  346.           p = index (dep_name (dep), '%');
  347.           if (p != 0)
  348.         {
  349.           register unsigned int i;
  350.           if (check_lastslash)
  351.             {
  352.               /* Copy directory name from the original FILENAME.  */
  353.               i = lastslash - filename + 1;
  354.               bcopy (filename, depname, i);
  355.             }
  356.           else
  357.             i = 0;
  358.           bcopy (dep_name (dep), depname + i, p - dep_name (dep));
  359.           i += p - dep_name (dep);
  360.           bcopy (stem, depname + i, stemlen);
  361.           i += stemlen;
  362.           strcpy (depname + i, p + 1);
  363.           p = depname;
  364.         }
  365.           else
  366.         p = dep_name (dep);
  367.  
  368.           /* P is now the actual dependency name as substituted.  */
  369.  
  370.           if (file_impossible_p (p))
  371.         {
  372.           /* If this dependency has already been ruled
  373.              "impossible", then the rule fails and don't
  374.              bother trying it on the second pass either
  375.              since we know that will fail too.  */
  376.           DEBUGP2 ("Rejecting impossible %s dependency `%s'.\n",
  377.                p == depname ? "implicit" : "rule", p);
  378.           tryrules[i] = 0;
  379.           break;
  380.         }
  381.  
  382.           intermediate_files[deps_found] = 0;
  383.  
  384.           DEBUGP2 ("Trying %s dependency `%s'.\n",
  385.                p == depname ? "implicit" : "rule", p);
  386.  
  387.           /* The DEP->changed flag says that this dependency resides in a
  388.          nonexistent directory.  So we normally can skip looking for
  389.          the file.  However, if CHECK_LASTSLASH is set, then the
  390.          dependency file we are actually looking for is in a different
  391.          directory (the one gotten by prepending FILENAME's directory),
  392.          so it might actually exist.  */
  393.  
  394.           if ((!dep->changed || check_lastslash)
  395.           && (lookup_file (p) != 0 || file_exists_p (p)))
  396.         {
  397.           found_files[deps_found++] = savestring (p, strlen (p));
  398.           continue;
  399.         }
  400.           /* This code, given FILENAME = "lib/foo.o", dependency name
  401.          "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
  402.           vp = p;
  403.           if (vpath_search (&vp, (time_t *) 0))
  404.         {
  405.           DEBUGP2 ("Found dependency `%s' as VPATH `%s'\n", p, vp);
  406.           strcpy(vp, p);
  407.           found_files[deps_found++] = vp;
  408.           continue;
  409.         }
  410.  
  411.           /* We could not find the file in any place we should look.
  412.          Try to make this dependency as an intermediate file,
  413.          but only on the second pass.  */
  414.  
  415.           if (intermed_ok)
  416.         {
  417.           if (intermediate_file == 0)
  418.             intermediate_file
  419.               = (struct file *) alloca (sizeof (struct file));
  420.  
  421.           DEBUGP2 ("Looking for a rule with %s file `%s'.\n",
  422.                "intermediate", p);
  423.  
  424.           bzero ((char *) intermediate_file, sizeof (struct file));
  425.           intermediate_file->name = p;
  426.           if (pattern_search (intermediate_file, 0, depth + 1,
  427.                       recursions + 1))
  428.             {
  429.               p = savestring (p, strlen (p));
  430.               intermediate_patterns[deps_found]
  431.             = intermediate_file->name;
  432.               intermediate_file->name = p;
  433.               intermediate_files[deps_found] = intermediate_file;
  434.               intermediate_file = 0;
  435.               /* Allocate an extra copy to go in FOUND_FILES,
  436.              because every elt of FOUND_FILES is consumed
  437.              or freed later.  */
  438.               found_files[deps_found] = savestring (p, strlen (p));
  439.               ++deps_found;
  440.               continue;
  441.             }
  442.  
  443.           /* If we have tried to find P as an intermediate
  444.              file and failed, mark that name as impossible
  445.              so we won't go through the search again later.  */
  446.           file_impossible (p);
  447.         }
  448.  
  449.           /* A dependency of this rule does not exist.
  450.          Therefore, this rule fails.  */
  451.           break;
  452.         }
  453.  
  454.       /* This rule is no longer `in use' for recursive searches.  */
  455.       rule->in_use = 0;
  456.  
  457.       if (dep != 0)
  458.         {
  459.           /* This pattern rule does not apply.
  460.          If some of its dependencies succeeded,
  461.          free the data structure describing them.  */
  462.           while (deps_found-- > 0)
  463.         {
  464.           register struct file *f = intermediate_files[deps_found];
  465.           free (found_files[deps_found]);
  466.           if (f != 0
  467.               && (f->stem < f->name
  468.               || f->stem > f->name + strlen (f->name)))
  469.             free (f->stem);
  470.         }
  471.         }
  472.       else
  473.         /* This pattern rule does apply.  Stop looking for one.  */
  474.         break;
  475.     }
  476.  
  477.       /* If we found an applicable rule without
  478.      intermediate files, don't try with them.  */
  479.       if (i < nrules)
  480.     break;
  481.  
  482.       rule = 0;
  483.     }
  484.  
  485.   /* RULE is nil if the loop went all the way
  486.      through the list and everything failed.  */
  487.   if (rule == 0)
  488.     return 0;
  489.  
  490.   foundrule = i;
  491.  
  492.   /* If we are recursing, store the pattern that matched
  493.      FILENAME in FILE->name for use in upper levels.  */
  494.  
  495.   if (recursions > 0)
  496.     /* Kludge-o-matic */
  497.     file->name = rule->targets[matches[foundrule]];
  498.  
  499.   /* FOUND_FILES lists the dependencies for the rule we found.
  500.      This includes the intermediate files, if any.
  501.      Convert them into entries on the deps-chain of FILE.  */
  502.  
  503.   while (deps_found-- > 0)
  504.     {
  505.       register char *s;
  506.  
  507.       if (intermediate_files[deps_found] != 0)
  508.     {
  509.       /* If we need to use an intermediate file,
  510.          make sure it is entered as a target, with the info that was
  511.          found for it in the recursive pattern_search call.
  512.          We know that the intermediate file did not already exist as
  513.          a target; therefore we can assume that the deps and cmds
  514.          of F below are null before we change them.  */
  515.  
  516.       struct file *imf = intermediate_files[deps_found];
  517.       register struct file *f = enter_file (imf->name);
  518.       f->deps = imf->deps;
  519.       f->cmds = imf->cmds;
  520.       f->stem = imf->stem;
  521.       imf = lookup_file (intermediate_patterns[deps_found]);
  522.       if (imf != 0 && imf->precious)
  523.         f->precious = 1;
  524.       f->intermediate = 1;
  525.       f->tried_implicit = 1;
  526.       for (dep = f->deps; dep != 0; dep = dep->next)
  527.         {
  528.           dep->file = enter_file (dep->name);
  529.           dep->name = 0;
  530.           dep->file->tried_implicit |= dep->changed;
  531.         }
  532.       num_intermediates++;
  533.     }
  534.  
  535.       dep = (struct dep *) xmalloc (sizeof (struct dep));
  536.       s = found_files[deps_found];
  537.       if (recursions == 0)
  538.     {
  539.       dep->name = 0;
  540.       dep->file = lookup_file (s);
  541.       if (dep->file == 0)
  542.         /* enter_file consumes S's storage.  */
  543.         dep->file = enter_file (s);
  544.       else
  545.         /* A copy of S is already allocated in DEP->file->name.
  546.            So we can free S.  */
  547.         free (s);
  548.     }
  549.       else
  550.     {
  551.       dep->name = s;
  552.       dep->file = 0;
  553.       dep->changed = 0;
  554.     }
  555.       if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
  556.     {
  557.       /* If the file actually existed (was not an intermediate file),
  558.          and the rule that found it was a terminal one, then we want
  559.          to mark the found file so that it will not have implicit rule
  560.          search done for it.  If we are not entering a `struct file' for
  561.          it now, we indicate this with the `changed' flag.  */
  562.       if (dep->file == 0)
  563.         dep->changed = 1;
  564.       else
  565.         dep->file->tried_implicit = 1;
  566.     }
  567.       dep->next = file->deps;
  568.       file->deps = dep;
  569.     }
  570.  
  571.   if (!checked_lastslash[foundrule])
  572.     /* Always allocate new storage, since STEM might be
  573.        on the stack for an intermediate file.  */
  574.     file->stem = savestring (stem, stemlen);
  575.   else
  576.     {
  577.       /* We want to prepend the directory from
  578.      the original FILENAME onto the stem.  */
  579.       file->stem = (char *) xmalloc (((lastslash + 1) - filename)
  580.                      + stemlen + 1);
  581.       bcopy (filename, file->stem, (lastslash + 1) - filename);
  582.       bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
  583.       file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
  584.     }
  585.  
  586.   file->cmds = rule->cmds;
  587.  
  588.   /* Put the targets other than the one that
  589.      matched into FILE's `also_make' member.  */
  590.  
  591.   /* If there was only one target, there is nothing to do.  */
  592.   if (rule->targets[1] != 0)
  593.     for (i = 0; rule->targets[i] != 0; ++i)
  594.       if (i != matches[foundrule])
  595.     {
  596.       struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
  597.       new->name = p = (char *) xmalloc (rule->lens[i] + stemlen + 1);
  598.       bcopy (rule->targets[i], p,
  599.          rule->suffixes[i] - rule->targets[i] - 1);
  600.       p += rule->suffixes[i] - rule->targets[i] - 1;
  601.       bcopy (stem, p, stemlen);
  602.       p += stemlen;
  603.       bcopy (rule->suffixes[i], p,
  604.          rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
  605.       new->file = enter_file (new->name);
  606.       new->next = file->also_make;
  607.       file->also_make = new;
  608.     }
  609.  
  610.  
  611.   return 1;
  612. }
  613.